package com.taskcollection.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by Administrator on 2016/8/13 0013.
 */
public class ConfigUtil {
    private static ConfigUtil configUtil = null;
    private JSONObject configjson;

    /**
     * 构造方法，读取配置文件
     */
    ConfigUtil(){
        //根据路径获取文件对象
        String path = "E:"+ File.separator+"config.json";
        File file = new File(path);
        try {
            //读取配置文件内容，并存入JSONObject中
            String configstr = FileUtils.readFileToString(file,"utf-8");
            configjson = JSON.parseObject(configstr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取单例对象
     * 单例类的对象@return
     */
    public static ConfigUtil getinstance(){
        if(configUtil==null){
            configUtil = new ConfigUtil();
        }
        return configUtil;
    }

    /**
     * 获取对应类的对象
     * 类名@param className
     * 类的一个对象@return
     */
    public Object getClass(String className){
        //如果配置文件为空，返回null
        if(configjson==null){
            return null;
        }
        //获取配置文件中的content中的内容
        JSONArray content = configjson.getJSONArray("content");
        //查找匹配对应类名，并根据类名创建对象
        for(int i = 0;i < content.size();i++){
            boolean flag = StringUtils.equalsIgnoreCase(content.getJSONObject(i).getString("name"), className);
            if(flag){
                String classjson = content.getJSONObject(i).getString("class");
                Object contentClass = createInstenceByClassName(classjson);
                if(content.getJSONObject(i).containsKey("properties")){
                    JSONArray properties = content.getJSONObject(i).getJSONArray("properties");
                    for(int j = 0;j < properties.size();j ++){
                        String propertiesName = properties.getJSONObject(j).getString("name");
                        String propertiesClass = properties.getJSONObject(j).getString("class");
                        Object propertiesInstance = createInstenceByClassName(propertiesClass);
                        String methName = "set"+StringUtils.capitalize(propertiesName);
                        try {
                            Method[] method = contentClass.getClass().getMethods();
                            for(Method m : method){
                                if(StringUtils.equals(methName,m.getName())){
                                    m.invoke(contentClass,new Object[]{propertiesInstance});
                                }
                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return contentClass;
            }
        }
        return null;
    }
    private Object createInstenceByClassName(String className){
        try {
            return Class.forName(className).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

}
